
********************************************************************************	
cd $pathdata_processed
use terminales_fin, clear

rename chance chance_o
rename predicted_gpa predicted_gpa_o

cd $pathdata_processed_cc
merge 1:1 npid using chance_cc, keep(1 3) nogen keepusing(chance predicted_gpa)
rename chance chance_cc
rename predicted_gpa predicted_gpa_cc

rename chance_o chance
rename predicted_gpa_o predicted_gpa

*
sum chance
replace chance = chance/`r(sd)'
replace expected_gpa = expected_gpa/`r(sd)'
replace predicted_gpa = predicted_gpa/`r(sd)'
*
sum chance_cc
replace chance_cc = chance_cc/`r(sd)'
replace predicted_gpa_cc = predicted_gpa_cc/`r(sd)'

preserve
cd $pathdata_processed
use terminales_fin, clear

keep matieres
duplicates drop
split matieres, p(,)

drop matieres
gen i = _n
reshape long matieres, i(i)
duplicates drop
drop if missing(matieres)
drop _j
sort matieres
levelsof matieres if _n == 1, c
local m1 `r(levels)'
levelsof matieres if _n == _N, c
local mN `r(levels)'
levelsof matieres, c
local matieres `r(levels)'
restore

replace matieres = "," + matieres + ","
foreach m in `matieres' {
	gen m`m' = strpos(matieres, ",`m',")>0
}
********************************************************************************
rename stdteachers_grades teach
rename stdteachers_grades2 teach2
	    
global controls teach teach2 stdms_gpa stdms_gpa2 m_ms_gpa ///
		 female ///
		 age age2 ///
		 p_age p_age2 m_mage m_fage ///
		 p_educ p_educ2 m_mbu_kl m_fbu_kl ///
		 ln_p_inc 			
************************************************************************
global partial_gpa i.year_vg3 i.skole_fo

gen all = 1
********************************************************************************
label var chance "$widetilde{\textrm{GPA_luck}}$"
label var predicted_gpa "$\textrm{GPA_luck}$"

label var chance_cc "$widetilde{\textrm{GPA_luck}}$ -- alternative definition of $Exam^e$"
label var predicted_gpa_cc "$\textrm{GPA_luck}$ -- alternative definition of $\textrm{Exam}^\textrm{e}$"

label var stdexam_grades "\hspace{0.2cm} Exam grades in 3\textsuperscript{rd} year"
label var stdvide3 "\hspace{0.2cm} High school GPA"
label var completion "\hspace{0.2cm} On time HS diploma"
label var hs_gs_c "\hspace{0.2cm} Ever HS diploma"
label var he_e "\hspace{0.2cm} Ever higher education"
label var share_available "\hspace{0.2cm} Share of available HE programs"
label var rank_f_he "\hspace{0.2cm} Selectivity of HE enrollment"
label var bu_sup "\hspace{0.2cm} Number of completed years in HE"
label var first_job "\hspace{0.2cm} Ever employed"
label var ln_wyrkinnt_c1 "\hspace{0.2cm} First job labor income (log)"
label var job10 "\hspace{0.2cm} Employed 8 years after the exams"
label var ln_wyrkinnt_c10 "\hspace{0.2cm} Labor income 8 years after the exams (log)"
********************************************************************************
cd $pathtable	
********************************************************************************
eststo clear
local seed 326553253
*******************************************************************************			
//Robustness checks				
*alternative controls			
foreach sample in all {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'l: ivlasso `var' (predicted_gpa = chance) ($controls $partial_gpa) if `sample' == 1, cluster(group) rob partial($partial_gpa)
				di "`var'"
				
				di "xselected_y"
				di "`e(xselected_y)'"
				
				di "xselected"
				di "`e(xselected)'"
				
				di "xselected_chs_pl"
				di "`e(xselected_chs_pl)'"
				
				di "xselected_chs_l"
				di "`e(xselected_chs_l)'"
				
				local c`var' "`e(xselected)'"
				}
			}		

foreach sample in all {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'sub: ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa m`m1'-m`mN' if `sample' == 1, cluster(group) rob
				}
			}
				
*Permutation tests
foreach sample in all {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'pgpa: ritest chance _b[chance], reps(99) nodots seed(`seed'): areg `var' chance $controls i.year_vg3 if `sample' == 1, cluster(group) rob absorb(skole_fo) 
				matrix pvalues=r(p)
				estadd scalar pvalue_treat = pvalues[1,1]
				}
			}
	
*Wild cluster bootstrap 
foreach sample in all {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'wb: ivreg2 `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob partial(i.skole_fo)
				boottest predicted_gpa, boottype(wild) cluster(group) robust nograph seed(`seed') reps(99) 
				gen p = r(p)
				sum p
				estadd r(mean): `sample'`var'wb
				drop p
				}
			}
			
*CC instrument 
foreach sample in all {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'cc: ivregress 2sls `var' (predicted_gpa_cc = chance_cc) $controls $partial_gpa if `sample' == 1, cluster(group) rob
				}
			}
			
*Controlling for expected_gpa 
foreach sample in all {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'bar: reg `var' predicted_gpa expected_gpa $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				}
			}			
				
		local sample "all"
		esttab `sample'stdvide3l `sample'job10l `sample'ln_wyrkinnt_c10l using "RB.tex", replace booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& \multicolumn{3}{c}{Outcomes} \\" "\cmidrule(lr){2-4}" "& High school GPA & Employed 8 years & Annual labor income \\" "& & after the exams & 8 years after the exams \\" "& & & (log) \\" "\midrule" "\multicolumn{4}{l}{\textit{Panel A: Controls for Students' Baseline Characteristics Selected by Double Lasso}} \\" "\addlinespace[1ex]") ///
		fragment label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
				
		local sample "all"
		esttab `sample'stdvide3sub `sample'job10sub `sample'ln_wyrkinnt_c10sub using "RB.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs posthead("\midrule" "\multicolumn{4}{l}{\textit{Panel B: Controls for Students' Courses in 3\textsuperscript{rd} year}} \\" "\addlinespace[1ex]") ///
		fragment label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		local sample "all"
		esttab `sample'stdvide3pgpa `sample'job10pgpa `sample'ln_wyrkinnt_c10pgpa using "RB.tex", append booktabs legend nolines ///
		drop(chance $controls 2005.year_vg3 2006.year_vg3 2007.year_vg3 2008.year_vg3 2009.year_vg3 _cons) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs posthead("\midrule" "\multicolumn{4}{l}{\textit{Panel C: P-values for $\widetilde{\textrm{GPA_luck}}$ Computed Using Permutation Tests}} \\" "\addlinespace[1ex]") ///
		fragment stats(pvalue_treat, label("P-values for $\textrm{GPA_luck}$") fmt(%9.3f 0) ) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none)
		
		local sample "all"
		esttab `sample'stdvide3wb `sample'job10wb `sample'ln_wyrkinnt_c10wb using "RB.tex", append booktabs legend nolines ///
		drop(predicted_gpa $controls 2006.year_vg3 2007.year_vg3 2008.year_vg3 2009.year_vg3) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs posthead("\midrule" "\multicolumn{4}{l}{\textit{Panel D: P-values for $\textrm{GPA_luck}$ Computed Using a Wild Bootstrap}} \\" "\addlinespace[1ex]") ///
		fragment stats(mean, label("P-values for $\textrm{GPA_luck}^\textrm{e}$") fmt(%9.3f 0) ) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
				
		local sample "all"
		esttab `sample'stdvide3cc `sample'job10cc `sample'ln_wyrkinnt_c10cc using "RB.tex", append booktabs legend ///
		keep(predicted_gpa_cc) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs posthead("\midrule" "\multicolumn{4}{l}{\textit{Panel E: Expected Exam Grades Computed Using Teacher Grades}} \\" "\addlinespace[1ex]") ///
		fragment label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		local sample "all"
		esttab `sample'stdvide3bar `sample'job10bar `sample'ln_wyrkinnt_c10bar using "RB.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs posthead("\midrule" "\multicolumn{4}{l}{\textit{Panel F: Using $\textrm{GPA_luck}$ while controlling for $\widebar{\textrm{GPA_luck}}$}} \\" "\addlinespace[1ex]") ///
		fragment label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 

foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
	di "`var'"
	di "`c`var''"
}		

		eststo clear		

preserve
clear
set obs 1
local tex "RB.tex"
local tex2 "RB.tex"

generate strL s = fileread("`tex'") if fileexists("`tex'")
assert filereaderror(s)==0

replace s = subinstr(s,"^\text&","^\textrm{e}$&", 1)

gen byte fw = filewrite("`tex2'",s,1)
restore	